var $;
layui.use(['layer', 'form', 'table', 'jquery', 'laydate'], function () {
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    $ = layui.jquery;
    var laydate = layui.laydate;
    laydate.render({
        elem: '#applyDate',
        type: 'date',
        trigger: 'click'
    });
    //加载表格
    getTable();

    function getTable() {
        $.ajax({
            url: '/record/getApplyRecord',
            type: 'post',
            dataType: 'json',
            data: {'position': 1},
            success: function (data) {
                tablereload(data.data);
            }
        });
    }

    var tableIns;

    function tablereload(data) {
        tableIns = table.render({
            elem: '#table',
            title: '申请列表',
            data: data,
            toolbar: '#apply',
            page: {
                limit: 10,
                layout: ['count', 'prev', 'page', 'next', 'skip', 'refresh'],
                first: "首页",
                last: "尾页",
            },
            cols: [[
                {type: 'checkbox', with: 50, fixed: 'left'},
                {
                    field: '', title: '序号', align: 'center', templet: function (data) {
                        return data.LAY_INDEX;
                    }
                },
                {field: 'fullName', title: '用户姓名', align: 'center'},
                {field: 'account', title: '用户账号', align: 'center'},
                {field: 'roleName', title: '申请职位', align: 'center'},
                {field: 'phone', title: '电话', align: 'center'},
                {field: 'applyDate', title: '申请时间', align: 'center'},
                {field: 'email', title: '邮箱', align: 'center'},
                {
                    field: 'result', title: '申请状态', align: 'center', templet: function (data) {
                        if (data.result == 1) {
                            return "未审核";
                        } else if (data.result == 2) {
                            return "已通过";
                        } else {
                            return "未通过";
                        }
                    }
                },
                {
                    fiele: '', title: '操作', align: 'center', templet: function (data) {
                        return "<div><a href='javascript:getinformation(" + data.launchId + ");' style='margin-right: 10px;color: #009688'>查看</a><a href='javascript:examineUser(\"" + data.id + "\",\"" + data.fullName + "\")' style='color: #009688'>审核</a></div>";
                    }
                }
            ]]
        });
    }

    //查看
    getinformation = function getinformation(userId) {
        $.ajax({
            url: '/user/getinformation',
            type: 'post',
            dataType: 'json',
            data: {'userId': userId},
            success: function (data) {
                devicesMsg(data.data);
            }
        });
    }

    function devicesMsg(data) {
        layer.open({
            type: 2,
            title: '用户信息',
            offset: 'auto',
            shade: 0,
            maxmin: true,
            area: ['1000px', '680px'],
            content: '/user/user-see.html',
            btn: ['关闭'],
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find('#avatarUrl').attr('src', data.avatarUrl);
                obj.find('input[name=account]').val(data.account);
                obj.find('input[name=nickname]').val(data.nickname);
                obj.find('input[name=userCode]').val(data.userCode);
                obj.find('input[name=studyDate]').val(data.studyDate);
                obj.find('input[name=freestyle]').val(data.freestyle);
                obj.find('input[name=idCard]').val(data.idCard);
                obj.find('input[name=phone]').val(data.phone);
                obj.find('input[name=sex]').val(data.sex);
            }
        });
    }

    //搜索
    form.on('submit(search)', function (data) {
        $.ajax({
            url: '/record/getApplyRecord?position=1',
            type: 'post',
            dataType: 'json',
            data: data.field,
            success: function (data) {
                tablereload(data.data);
            }
        });
    });
    //审核
    examineUser = function examineUser(id, fullName) {
        layer.open({
            type: 2,
            title: '创作者用户申请',
            shade: 0,
            offset: 'auto',
            maxmin: true,
            area: ['452px', '350px'],
            btn: ['确定', '取消'],
            content: '/user/user-examine.html',
            success: function (layero, index) {
                var obj = layero.find('iframe').contents();
                obj.find('#fullName').text(fullName);
            },
            yes: function (index, layero) {
                var obj = layero.find('iframe').contents();
                var result = obj.find('#isAdopt input[name=result]:checked').val();
                var remarks = obj.find('#remarks').val();
                $.ajax({
                    url: '/record/updateApplyRecord',
                    type: 'post',
                    dataType: 'json',
                    data: {'id': id, 'result': result, 'remarks': remarks},
                    success: function (data) {
                        if (data.data) {
                            if (result == 2) {
                                result = 2;
                            } else {
                                result = 1;
                            }
                            var userId = localStorage.getItem('createId');
                            var jsAuditLog = {
                                'tableName': 'js_apply_record',
                                'type': result,
                                'userId': userId,
                                'remark': remarks,
                                'aduitLogType': 4,
                                'mainId': id
                            }
                            $.ajax({
                                url: '/auditLog/addAuditLog',
                                type: 'post',
                                dataType: 'json',
                                data: jsAuditLog,
                                success: function (data) {
                                    if (data.data) {
                                        layer.msg("审核成功");
                                        layer.close(index);
                                        getTable();
                                    }
                                }
                            });
                        }
                    }
                });
            }
        });
    }
});

//返回
function goBack() {
    //通过页面查找iframe
    parent.$('#recordLog').val($('#application').text());
    parent.before();
    return false;
}